package cn.burnit.service;

import cn.burnit.pojo.User;
import cn.burnit.utils.ConnectUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 用户jdbc方法
 */
public class UserService {



    public User login(String username, String password, String is_admin) {
        ConnectUtils.getDriver();
        Connection connection=null;

        try {
            connection=ConnectUtils.getConnection();
            String sel="select * from user where username=? and password=? and is_admin=?";
            PreparedStatement ps = connection.prepareStatement(sel);
            ps.setString(1,username);
            ps.setString(2,password);
            ps.setString(3,is_admin);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()){
                User user = new User();
                username = resultSet.getString(1);
                password=resultSet.getString(2);
                is_admin=resultSet.getString(3);
                user.setUsername(username);
                user.setPassword(password);
                user.setIs_admin(is_admin);
                return user;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public User findUserByUsername(String username) {
        ConnectUtils.getDriver();
        Connection connection=null;

        try {
            connection=ConnectUtils.getConnection();
            String findSql="select * from user where username=?";
            PreparedStatement ps = connection.prepareStatement(findSql);
            ps.setString(1,username);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()){
                User user = new User();
                username = resultSet.getString(1);
                user.setUsername(username);
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public int updatePwd(User user) {
        int i=0;
        ConnectUtils.getDriver();
        Connection connection=null;

        try {
            connection=ConnectUtils.getConnection();
            String updateSql="update user set password=? where username=?";
            PreparedStatement ps = connection.prepareStatement(updateSql);
            ps.setString(1,user.getPassword());
            ps.setString(2,user.getUsername());
            i = ps.executeUpdate();
            if (i>0){
                System.out.println("密码更新成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    public int addUser(String username, String password, String is_admin) {
        int i=0;
        ConnectUtils.getDriver();
        Connection connection=null;
        try {
            connection=ConnectUtils.getConnection();
            String addUserSql="insert into user values(null,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(addUserSql);
            ps.setString(1,username);
            ps.setString(2,password);
            ps.setString(3,is_admin);
            i = ps.executeUpdate();
            if (i>0){
                System.out.println("user添加成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
}
